package org.springframework.core.annotation;

import lombok.Data;
import lombok.SneakyThrows;
import lombok.ToString;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Objects;

@Data
public class AnnotationAttributeMethod {
    private Annotation annotation;
    private Method method;
    private Object defaultValue;
    private Object value;
    private boolean userValue;

    @SneakyThrows
    public AnnotationAttributeMethod(Annotation annotation,
                                     Method method) {
        this.annotation = annotation;
        this.method = method;
        this.defaultValue = method.getDefaultValue();
        this.value = method.invoke(annotation);
        this.userValue = !Objects.equals(this.defaultValue, this.value);
    }

    static AnnotationAttributeMethod of(Annotation annotation,
                                        Method method) {
        return new AnnotationAttributeMethod(annotation, method);
    }

    @Override
    public String toString() {
        return "AnnotationAttributeMethod{method=" + method + ", defaultValue=" + defaultValue + ", value=" + value + ", userValue=" + userValue + '}';
    }
}
